package com.tanqidi.arcgisandroid.ui.envConfig

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.tanqidi.arcgisandroid.R
import com.tanqidi.arcgisandroid.base.utils.SPUtils
import com.tanqidi.arcgisandroid.base.utils.getSPUtils
import com.tanqidi.arcgisandroid.databinding.FragmentEnvConfigBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class EnvConfigFragment : Fragment() {

    private val viewModel: EnvConfigViewModel by viewModels()
    private lateinit var binding:FragmentEnvConfigBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = FragmentEnvConfigBinding.inflate(layoutInflater)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        initTopBar()
        initView()
        return binding.root
    }

    private fun initTopBar() {
        binding.topAppBar.setNavigationOnClickListener {
            findNavController().navigateUp()
        }
    }

    private fun initView() {
        val basemapTypeList = listOf(getString(R.string.mapbox), getString(R.string.arcgis), getString(R.string.tiandi))
        val instance = getSPUtils()
        binding.baseMap.apply {
            setText(instance.getString(getString(R.string.basemap), null), false)
            val arrayAdapter = ArrayAdapter(requireContext(), R.layout.cat_exposed_dropdown_popup_item, basemapTypeList)
            setAdapter(arrayAdapter)
            setOnItemClickListener { parent, view, position, id ->
                instance.put(getString(R.string.basemap), basemapTypeList[position])
            }
        }
    }

}